﻿using Sidvall.Collections;
using Sidvall.Data;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sidvall.LogManager.Model.DataPortalManagers
{
    public partial class LogScopeItemManager<TEntity, TList>
    {
        #region Public Members

        #region DeleteLogScopeItemsAsync

        public async Task<int> DeleteLogScopeItemsAsync(int? serverConnectionId, long? logScopeId, Sidvall.Logging.EventType? eventType,
            string category, string textFilter, System.DateTime? expiry)
        {
            var criteria = CriteriaManager.NewDeleteCriteria<Sidvall.LogManager.Data.Fields.LogScopeItemField>(ParameterManager.DeleteEventItemsCriteriaId, null);

            // Filter
            PrepareCriteriaFilter(criteria, serverConnectionId, logScopeId, eventType, category, textFilter, expiry);

            return await DeleteItemsAsync(criteria).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsAsync

        public async Task<TList> GetItemsAsync(int? serverConnectionId, long? logScopeId, Sidvall.Logging.EventType? eventType,
            string category, string textFilter, System.DateTime? expiry,
            int? rowCount, int? rowStartIndex, IEnumerable<Sidvall.Data.SortItem> sortItems)
        {
            var criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<Data.Fields.LogScopeItemField, Data.Relations.LogScopeItemRelation>(ParameterManager.GetEventItemsCriteriaId, null);
            if (rowCount != null)
            {
                criteria.RowCount = rowCount.Value;
                criteria.RowStartIndex = rowStartIndex;
            }

            // Sort
            criteria.SortItems = new SortItemCollection<Data.Fields.LogScopeItemField>();
            criteria.SortItems.AddRange(sortItems);
            if (criteria.SortItems.IsNullOrEmpty())
            {
                criteria.SortItems.AddCustom(nameof(Data.Fields.LogScopeItemField.CreatedTime), false);
                criteria.SortItems.AddCustom(nameof(Data.Fields.LogScopeItemField.Title));
            }

            // Filter
            PrepareCriteriaFilter(criteria, serverConnectionId, logScopeId, eventType, category, textFilter, expiry);

            // Load items
            var items = await GetItemsAsync(criteria).ConfigureAwait(false);
            var categoryCriteria = new Sidvall.Data.FetchListCriteria<Data.Fields.LogScopeItemCategoryField, Data.Relations.LogScopeItemCategoryRelation>();
            ParameterManager.InitializeParameters(categoryCriteria, serverConnectionId);
            await LoadLogScopeItemCategoriesAsync(items.DataItems, false, false, categoryCriteria, null, null).ConfigureAwait(false);

            return items;
        }

        #endregion

        #endregion
        #region Private Members

        #region PrepareCriteriaFilter

        private static void PrepareCriteriaFilter(FilterCriteriaBase<Sidvall.LogManager.Data.Fields.LogScopeItemField> criteria,
            int? serverConnectionId, long? logScopeId, Sidvall.Logging.EventType? eventType, string category, string textFilter, System.DateTime? expiry)
        {
            // Filter
            criteria.Filter = new Sidvall.Data.Filter<Data.Fields.LogScopeItemField>();
            if (expiry != null)
                criteria.Filter.AddFilterItem(Data.Fields.LogScopeItemField.CreatedTime, Sidvall.Data.ComparisonOperator.LessThan, expiry.Value);

            // Parameters
            criteria.Parameters = new ParameterCollection();
            if (logScopeId != null)
                criteria.Parameters.Add(Data.Fields.LogScopeItemField.LogScopeId.ToString(), logScopeId.Value);
            if (eventType != null)
                criteria.Parameters.Add(Data.Fields.LogScopeItemField.EventTypeId.ToString(), eventType.Value);
            if (!string.IsNullOrWhiteSpace(textFilter))
                criteria.Parameters.Add(Data.Fields.LogScopeItemField.Message.ToString(), textFilter);
            if (!string.IsNullOrWhiteSpace(category))
                criteria.Parameters.Add(Data.Fields.LogScopeItemCategoryField.Category.ToString(), category);
            ParameterManager.InitializeParameters(criteria, serverConnectionId);
        }

        #endregion

        #endregion
    }
}